<?php

namespace App\Console\Commands\Common;

use App\Models\Common\CityResidencePriceModel;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Http;

class UpdateCityResidenceProvince extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'common:update-city-residence-province';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '更新小区对应省市区-通过经纬度查询';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return void
     */
    public function handle()
    {

        //更新小区对应省市区-通过经纬度查询
        CityResidencePriceModel::query()
            ->where('district','')
            ->chunk(500, function ($cityResidencePriceModel) {
                foreach ($cityResidencePriceModel as $v) {
                    if ($v['centerLongitude'] && $v['centerLatitude']) {
                        $areaRes = Http::withHeaders(['Content-Type' => 'application/json; charset=utf-8'])
                            ->get(sprintf(config('area.getAreaUrlGaoDeV3'), $v['centerLongitude'], $v['centerLatitude']))
                            ->json();

                        if (($areaRes['status'] ?? -1) == 1) {
                            var_dump($areaRes['regeocode']['addressComponent']);//高德 企业认证每天3000000
                            $province = $areaRes['regeocode']['addressComponent']['province'] ?? "";
                            $city     = $areaRes['regeocode']['addressComponent']['city'] ?? "";
                            $district = $areaRes['regeocode']['addressComponent']['district'] ?? "";
                            if (($city == '' || !$city) && in_array($province, ['上海市', '北京市', '重庆市', '天津市'])) {
                                $city = $province;
                            }

                            if (!$district || !mb_strlen($district)) {//针对东莞的特殊算法
                                $district = $areaRes['regeocode']['addressComponent']['township'] ?? '';
                            }
                            var_dump($province);
                            var_dump($city);
                            var_dump($district);
                            if ($province && $city && $district) {
                                CityResidencePriceModel::query()->where('id', $v->id)
                                    ->update(
                                        [
                                            'province' => $province,
                                            'city'     => $city,
                                            'district' => $district,
                                        ]
                                    );
                            }
                        } else {
                            var_dump($areaRes);
                            sleep(0.5);
                        }
                    }
                }
            });
    }
}
